package com.woniuxy.carlife.carlifeuserweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.carlifecommonsweb.exception.CarlifeGlobalException;
import com.woniuxy.carlife.carlifeuserweb.entity.Shoppingcart;
import com.woniuxy.carlife.carlifeuserweb.mapper.ShoppingcartMapper;
import com.woniuxy.carlife.carlifeuserweb.service.ShoppingcartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.carlife.carlifeuserweb.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zjq
 * @ClassName:ShoppingcartServiceImpl
 * @Description:
 * @date 2021/4/6 20:02
 **/

@Slf4j
@Service
public class ShoppingcartServiceImpl extends ServiceImpl<ShoppingcartMapper, Shoppingcart> implements ShoppingcartService {

    @Resource
    ShoppingcartMapper shoppingcartMapper;

    @Resource
    StatisticsService statisticsService;

    /*
     * 功能描述〈用户查询购物车〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/12 11:54
     * @throws
     */

    @Override
    public List<Shoppingcart> findShoppingcartByUserId(Integer userId) {
        QueryWrapper<Shoppingcart> shoppingcartQueryWrapper = new QueryWrapper<>();
        shoppingcartQueryWrapper.eq("user_id", userId);
        List<Shoppingcart> shoppingcarts = shoppingcartMapper.selectList(shoppingcartQueryWrapper);

        return shoppingcarts == null ? null : shoppingcarts;
    }


    /*
     * 功能描述〈用户新增购物车〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 4:53
     * @throws
     */

    @Override
    public boolean addShoppingcartByUserId(Shoppingcart shoppingcart) {
        log.info("执行新增购物车mysql-plues语句");
        int insert = shoppingcartMapper.insert(shoppingcart);
        if (insert > 0) {
            boolean b = statisticsService.modifyShoppingCart(shoppingcart.getUserId());
            if (b) {
                log.info("执行完毕新增购物车mysql-plues语句");
                return true;
            }
        }


        return false;
    }

    /*
     * 功能描述〈用户删除购物车〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/13 4:54
     * @throws
     */

    @Override
    public boolean deleteShoppingcartByShoppingcartId(Integer shoppingcartId, Integer userId) {
        log.info("开始执行删除购物车");
        int i = shoppingcartMapper.deleteById(shoppingcartId);
        if (i > 0) {
            boolean b = statisticsService.reduceShoppingCart(userId);
            if (b) {
                log.info("开始执行删除购物车完毕");
                return true;
            }

        }

        return false;
    }




    /*
     * 功能描述〈统计用户购物车数量〉
     * @author:zjq
     * @param:
     * @return:
     * @Date 2021/4/15 15:53
     * @throws
     */

    @Override
    public Integer shoppingcartTotalByUserId(Integer userId) {
        QueryWrapper<Shoppingcart> shoppingcartQueryWrapper = new QueryWrapper<>();
        // QueryWrapper<Employee> queryWrapper2=Wrappers.<Employee>query();
        shoppingcartQueryWrapper.eq("user_id",userId);
        Integer count = shoppingcartMapper.selectCount(shoppingcartQueryWrapper);
        return count;
    }


}
